<?php



class SvcExportConnected extends SvcCommun {
    
    
    
    
    //ce nom de dossier apparaitra dans les zip d'export
    //en changeant ce nom, les précédentes sauvegardes deviennent incompatibles
    private static $tempDir='cinema/'; 
    
    
    public static function verifier(){
	
	if(!Security::isConnected()){
	    Security::notAllowedPage();
	}
	
	$action=Form::getParam('action');
	if($action=='backup'){
	    self::doBackup();
	}
	elseif($action=='restore'){
	    self::doRestore();
	}
	elseif($action=='export'){
	    self::doExport();
	}
	
	self::$title='Sauvegarde et export';

    }
    
    
    private static function doBackup(){
        Message::log('Lancement d\'une sauvegarde');
        $tempDir=self::$tempDir;
        
        FileSystem::clearDirectory($tempDir);
        FileSystem::createDirectory($tempDir.'img/');
        FileSystem::createDirectory($tempDir.'logs/');
        
        $file=$tempDir.'backup.sql';
        FileSystem::write($file,Backup::doBackup());
        
        FileSystem::copyDir('data/img/',$tempDir.'img/');
        FileSystem::copyDir('data/logs/',$tempDir.'logs/');
        FileSystem::copyFile('cfg/parametrage.cfg.ini',$tempDir.'parametrage.cfg.ini');
        
        $date=date('YmdHis');
        $zip=$tempDir."sauvegarde-$date.zip";
        Zip::zipDir($tempDir, $zip);
        
        Message::log('Sauvegarde terminée');
        Download::send($zip);
    }
    
    
    private static function doRestore(){
	Message::log('Restauration d\'une sauvegarde');
	$tempDir=self::$tempDir;
	
	FileSystem::removeDirectory($tempDir);
	
	$zip=Upload::check('backup',array('zip'));
	
	if(Zip::unzipFile($zip)){
	    FileSystem::createDirectory($tempDir);
	    FileSystem::copyFile($tempDir.'parametrage.cfg.ini','cfg/parametrage.cfg.ini');
	    FileSystem::clearDirectory('data/logs/');
	    FileSystem::copyDir($tempDir.'logs/','data/logs/');
	    FileSystem::clearDirectory('data/img/');
	    FileSystem::copyDir($tempDir.'img/','data/img/');
	    Backup::restore($tempDir.'backup.sql');
	}
	FileSystem::removeDirectory($tempDir);
	
	Message::info('Restauration terminée');
    }
    
    
    private static function doExport(){
	Message::log('Lancement d\'un export');
	$tempDir=self::$tempDir;
	$tree=array(
	    'cfg'=>array(
		'default'=>''
	    ),
	    'data'=>array(
		'img'=>'',
		'logs'=>''
	    ),
	    'php'=>array(
		'controler'=>'',
		'model'=>'',
		'service'=>array(
		    'connect'=>''
		),
		'template'=>'',
		'view'=>''
	    ),
	    'static'=>array(
		'css'=>'',
		'img'=>'',
		'js'=>''
	    )
	);
	FileSystem::clearDirectory($tempDir);
	FileSystem::makeTree($tree,$tempDir);
	
        FileSystem::copyDir('cfg/default/',$tempDir.'cfg/default/');
        FileSystem::copyDir('php/controler/',$tempDir.'php/controler/');
        FileSystem::copyDir('php/model/',$tempDir.'php/model/');
        FileSystem::copyDir('php/service/',$tempDir.'php/service/');
        FileSystem::copyDir('php/service/connect/',$tempDir.'php/service/connect/');
        FileSystem::copyDir('php/template/',$tempDir.'php/template/');
        FileSystem::copyDir('php/view/',$tempDir.'php/view/');
        FileSystem::copyDir('static/css/',$tempDir.'static/css/');
        FileSystem::copyDir('static/img/',$tempDir.'static/img/');
        FileSystem::copyDir('static/js/',$tempDir.'static/js/');
        FileSystem::copyDir('./',$tempDir);
	
	$file=$tempDir.'dump.sql';
	FileSystem::write($file,Backup::dump());
	
	$date=date('YmdHis');
	$zip=$tempDir."export-cinema-$date.zip";
	Zip::zipDir($tempDir, $zip);

	Message::warn('Le dump de la base n\'a pas encore été implémenté');
	
	Message::log('Export terminé');	
	Download::send($zip);
    }
    
    
}


?>